% GAUTIER LE BIHAN - 2020
% Replication files for �Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model� �Review of Economics and Statistics
%
% This code produces   Appendix figure I based on stored  simulated data
clear;
tic

clc;
cd ..\..\Simu_identification\ident_canova_sala_MC
close all


load mat_results_CS
param=mat_results_CS;
moments=[ 0.0837  0.6737  0.0438  0.0698  4.4616];
 
actual_std=[ 0.0008  0.0042  0.0005  0.0010  0.1057]
actual_var= actual_std.* actual_std*1000;

 
m_target = moments';
m_scale=[actual_var'];
s=size(param,1);

for jj=1:s;
m_model=param(jj,4:8)';
G = m_target - m_model;
% % Weighting matrix W
size_weights = max(size(m_target));
scale = (ones(size_weights,1).*[4;1;1;1;1])./m_scale; 
% % take probability of default with 'double importance'
 W = eye(size_weights).*(ones(size_weights,1)*scale');
 clear scale size_weights
% 
 dist_sqrd = G.*G;
 W_vector = [W(1,1);W(2,2);W(3,3);W(4,4);W(5,5)];
dist_weighted = dist_sqrd.*W_vector;
% % Calculate new SMM estimator:
% 
 x(jj,1) = G'*W*G;
end;
param=[param x];

dist=param(:,3);

%figure;
%scatter(param1(param1(:,6)>0.082,5),param1(param1(:,6)>0.082,11))
%figure;scatter(param1(:,5),param1(:,11))


mylevel=-1*[0.7 0.755 0.80 0.85 0.9 ];

param1=param((dist<0.95 & dist>0 ) ,:);
%& param(:,6)>0.082%
ncontour=4
paramopt=param((dist==min(dist((dist>0),1)) ),:);

param2=param1((param1(:,1)~= paramopt(1)),:);
dist2=param2(:,3);
paramopt2=param2((dist2==min(dist2) ),:);



%paramopt=[0.0503 0.0303 0.0344 0.694600000000000 0.00794312795346131]
%param0=[0.0501    0.0373   0.0381   0.6946];

M=[param1(:,1) param1(:,2) -1*param1(:,3)];
x = linspace(min(M(:,1)), max(M(:,1)), size(M,1)+1);
y = linspace(min(M(:,2)), max(M(:,2)), size(M,1)+1);
[X,Y] = meshgrid(x, y);
Z = griddata(M(:,1), M(:,2), M(:,3), X, Y);
figure(1)

[C,h]=contourf(X, Y, Z, ncontour);
h.LevelList=mylevel;

map =  [
%ext�rieur   
 0.8 0.8 0.8
%2nd stage
      0.6 0.6 0.6
      0.6 0.6 0.6
      %3rd stage
      0.4 0.4 0.4 
      0.4 0.4 0.4 
      %Center
      0 0 0
       %0 0 1 
    
    ];
% map =  [
% %ext�rieur   
% 0 0.0 0.0
% %2nd stage
%       1 0 0.
%       1 0 0.
%       %3rd stage
%       00. 1 0.
%       00. 1 0.
%       %Center
%       00. 0 1
%        %0 0 1 
%     
%     ];
%surf(peaks)
colormap(map)
%colormap(pink(5))
cmap = colormap
x0=paramopt(:,1);
y0=paramopt(:,2);
%return;
sz=50;
c=[1 0 0];
% hold on
% scatter(x0, y0,sz,c, 'filled');
% hold off
sz=30;
 hold on
 scatter(paramopt(:,1), paramopt(:,2),sz,c, 'filled');
 hold off
% hold on
% scatter(param2, y1,sz,c, 'filled');
% hold off
xmu=[0.024 0.036]
ysig=[0.032 0.038]

%axis([xmu ysig]);
xlabel('\mu') % y-axis label
ylabel('\sigma') % x-axis label

%contour(X, Y, Z, 5, 'ShowText','on')
%axis equal
%grid
 print('..\..\..\figures\Canova_MC.pdf','-dpdf', '-fillpage')



figure(7);
%P0
x=param1(:,1);
y=param1(:,2);
z=param1(:,3);


x0=paramopt(:,1);
y0=paramopt(:,2);


scatter(y, x);
lsline

[b,bint] = regress(y,[ones(size(x)) x])
b 
bint
lm = fitlm([x],y,'linear')

% hold on
% scatter(y0, x0,sz,c, 'filled');
% hold off
x1=[0.01  0.06];
y1=[0.04  0.06];


%axis([x1 y1])
ylabel('\mu') % x-axis label
xlabel('\sigma') % y-axis label

